package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.MovieBean;
import com.project.bean.OrderBean;
import com.project.bean.UserBean;
import com.project.service.IMovieService;
import com.project.service.IOrderService;
import com.project.service.IUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RequestMapping("sell")
@RestController
public class sellController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IMovieService movieService;

    @Autowired
    private IOrderService orderService;

    @RequestMapping("login")
    public UserBean login(String name, String password, HttpServletRequest req) {
        UserBean loginUser = userService.login(name, password);
        req.getSession().setAttribute("loginUser", loginUser);
        return loginUser;
    }

    @RequestMapping("findByItem")
    public IPage<MovieBean> findByItem(Integer pageNO, String name, String cinemaName, String time, Integer rowNum, String status) {
        return movieService.findByItem(pageNO, name, cinemaName, time, rowNum, status);
    }

    @RequestMapping("buyMovie")
    public String buyMovie(Integer movieId, HttpServletRequest req) {
        UserBean loginUser = (UserBean) req.getSession().getAttribute("loginUser");
        MovieBean movie = movieService.findById(movieId);
        orderService.addOrder(new OrderBean(loginUser, new MovieBean(movie.getName(), movie.getTime(), movie.getCinemaName(), movie.getRowNum(), movie.getSeatNum())));
        movieService.buyMovie(movie);
        return "ok";
    }

    @RequestMapping("findMyOrder")
    public List<OrderBean> findMyOrder(HttpServletRequest req) {
        UserBean loginUser = (UserBean) req.getSession().getAttribute("loginUser");
        return orderService.findMyOrder(loginUser.getName());
    }
}
